<?php

namespace App\Services\MakeBoxProductOrder;

use App\Models\User;
use App\Models\Product;
use App\Models\Address;
use App\Models\BlindBoxOrder;
use App\Models\BlindBoxProduct;
use App\Models\BlindBoxLevelValue;
use App\Models\ProductSku;
use Brick\Math\RoundingMode;
use App\Events\OrderCreatedEvent;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use App\Services\MakeProductOrder\MakeProductOrder;

class takeBoxProductOrder
{
	protected ?User $user = null;

	protected ?Address $address = null;

	protected ?BlindBoxOrder $order = null;

	/**
	 * sku_id, attr_value_index, amount.
	 */

	public function __construct(
		User $user,
		Address $address,
		BlindBoxOrder $order)
	{
		$this->user = $user;
		$this->address = $address;
		$this->order = $order;
	}

	protected function items(): array
	{
		return [[
			'sku_id' => $this->order->sku_id,
			'amount' => 1
		]];
	}


	protected function create()
	{
		return DB::transaction(function () {
			
            $order = app(MakeProductOrder::class, [
				'user' => $this->user,
				'address' => $this->address,
				'items' => $this->items(),
				'attr' => null,
				'order' => $this->order,
				'isFree' => 0,
            	'userCouponId' => $this->order->user_coupon_id, 
			])->make();

            $this->order->update(['is_taken' => true]);
			return $order;
		});
	}

	
	public function take()
	{
		return $this->create();
	}
}
